/**
 * 
 */
package com.qjdchina.sku.front.web.interceptor;

import com.alibaba.fastjson.JSON;
import com.qjdchina.common.web.util.RestBody;
import com.qjdchina.sku.consts.WebConsts;
import com.qjdchina.sku.service.enums.SkuResultCode;
import com.sun.org.apache.xpath.internal.operations.String;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 
 * 类名: AuthorizeInterceptor <p>  
 * 功能: 权限拦截器 <p>  
 * 日期: 2015年9月18日 下午12:45:22 <p>  
 * @author wangzheng  
 * @version 1.0.0
 * @since  JDK 1.7  
 * @see
 */
public class AuthorizeInterceptor implements HandlerInterceptor {

    private static final Logger LOGGER = LoggerFactory.getLogger(AuthorizeInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession httpSession = request.getSession();
        Object attribute = httpSession.getAttribute(WebConsts.USER_ID);
        if(null == attribute){
            response.getWriter().print(JSON.toJSON(new RestBody<java.lang.String>(SkuResultCode.SESSION_NOT_EXIST)));
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
